<?php

class CompanyStatsController extends BaseController
{
	
	//商家销量统计
	public function actionAdmin()
	{
		$model = new Company();
		$model->unsetAttributes();
		$model->zc_deposit_status = 2;//保证金审核状态：1.未审核，2审核通过
		$model->zc_status = 2;//审核通过
		$model->list_order = 2;//销量倒序
		//获取筛选条件
		if(isset($_GET['Company'])){
			$model->attributes = $_GET['Company'];
			if(!empty($_GET['Company']['cateid2'])){
				$model->category_top = (int)$_GET['Company']['cateid2'];
			}else{
				$model->category_top = (int)$_GET['Company']['cateid1'];
			}
		}

		$this->render('admin',array(
				'model'=>$model
		));
	}
	
	//数据导出
	public function actionExport(){
		$model = new Company();
		$model->unsetAttributes();
		$model->zc_deposit_status = 2;//保证金审核状态：1.未审核，2审核通过
		$model->zc_status = 2;//审核通过
	
		//获取筛选条件
		if(isset($_GET['Company'])){
			$model->attributes = $_GET['Company'];
			if(!empty($_GET['Company']['cateid2'])){
				$model->category_top = (int)$_GET['Company']['cateid2'];
			}else{
				$model->category_top = (int)$_GET['Company']['cateid1'];
			}
		}
		$criteria=new CDbCriteria;
		//根据一级行业分类查找商铺
		if(!empty($model->category_top)){
			$catetop = Category::model()->findByPk($$model->category_top);
			$criteria->join = ' LEFT JOIN {{category}} cat ON cat.zca_id=t.zca_id';
			$criteria->addCondition(" (t.zca_id={$model->category_top} OR (t.zca_id IN ({$catetop->zca_arrchildid}))) ");
		}else{
			$criteria->compare('zca_id',$model->zca_id);
		}
		$criteria->compare('zc_name',$model->zc_name);
		//销量倒序
		$criteria->join .= ' LEFT JOIN {{ord_product}} op ON op.zm_id=t.zm_id';
		$criteria->select = "t.*,SUM(op.zop_nums) as opsum ";
		$criteria->group = "zm_id";
		$criteria->order = " opsum DESC ";

		$companys = Company::model()->findAll($criteria);
		$this->toExcel($companys,array('zm_id','zc_name','zc_sub_domain','zca_id','inputtime:Datetime','productCount','orderCount'),'公司销量排行榜');
		exit;
	}
	
	
	public function behaviors()
	{
		return array(
				'eexcelview'=>array(
						'class'=>'ext.eexcelview.EExcelBehavior',
				),
		);
	}
}